package com.jiancong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiancong.domain.ResponseResult;
import com.jiancong.domain.entity.Article;
import com.jiancong.domain.entity.Comment;
import com.jiancong.domain.entity.LoginUser;
import com.jiancong.domain.entity.User;
import com.jiancong.domain.vo.ArticleListVo;
import com.jiancong.domain.vo.CommentVo;
import com.jiancong.domain.vo.PageVo;
import com.jiancong.enums.AppHttpCodeEnum;
import com.jiancong.exception.SystemException;
import com.jiancong.mapper.ArticleMapper;
import com.jiancong.mapper.CommentMapper;
import com.jiancong.mapper.UserMapper;
import com.jiancong.service.CommentService;
import com.jiancong.utils.RedisCache;
import com.jiancong.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult commentList(String commentType,Long articleId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //查询articleId在comment表中是根评论的数据
        List<CommentVo> commentVoList = commentMapper.selectCommentVoList(commentType,articleId);

        List<CommentVo> commentVoList1 = toCommentVoList(commentVoList);
        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        for (CommentVo commentVo : commentVoList1) {
            //查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);
        }

        PageInfo<CommentVo> pageInfo =  new PageInfo<>(commentVoList);
        List<CommentVo> commentVos = pageInfo.getList();
        PageVo pageVo = new PageVo(commentVos,pageInfo.getTotal());
        System.out.println(pageVo);
        return ResponseResult.okResult(pageVo);
    }

    @Override
    @Transactional
    public ResponseResult addComment(Comment comment) {
        //评论内容不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        Long userId = SecurityUtils.getUserId();
        comment.setCreateBy(userId);
        comment.setUpdateBy(userId);
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        commentMapper.save(comment);


        return ResponseResult.okResult();
    }
    /**
     * 根据根评论的id查询所对应的子评论的集合
     * @param id 根评论的id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {

        List<CommentVo> children = commentMapper.getChildren(id);
        List<CommentVo> commentVoList = toCommentVoList(children);
        return commentVoList;
    }

    private List<CommentVo> toCommentVoList(List<CommentVo> commentVoList) {
        for (CommentVo commentVo : commentVoList) {
            String nickName =
                    userMapper.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            if (commentVo.getToCommentId() != -1) {
                //通过creatyBy查询用户的昵称并赋值
                User user = userMapper.getById(commentVo.getToCommentUserId());
                commentVo.setToCommentUserName(user.getNickName());
            }
        }
        return commentVoList;
    }
}